<?php
class Model_Product extends MVC_MODEL {
/**
 * @var object SQL
 */
    public $oDb_Product;
    private $rsCatRootPath;

    public function __construct() {
        parent::__construct();
        $this->oDb_Product = new Db_product();
        $this->rsCatRootPath=array();
    }
    public function getListProduct() {
        return $this->oDb_Product->getListProduct($this);
    }
    public function getListProvince() {
        return $this->oDb_Product->getListProvince($this);
    }
    public function getOneProvince($product_province_id) {
        return $this->oDb_Product->getOneProvince($product_province_id);
    }
    public function do_insert_new_product() {
        return $this->oDb_Product->doInsert($this->getAllData(),true);
    }
    public function deleteProduct() {
        return $this->oDb_Product->doDeleteByPK($this->getData('product_id'));;
    }
    public function deleteImageOfProduct() {
        return $this->oDb_Product->deleteImageOfProduct($this->getData('product_id'));;
    }
    public function deleteProAtt() {
        return $this->oDb_Product->deleteProAtt($this);
    }
    //=========Edit by Nam==
    public function updateProductDefaul($_POST,$_POST_name) {
        return $this->oDb_Product->updateProductDefaul($_POST,$_POST_name);
    }
    public function getInfoProductDefaul() {
        return $this->oDb_Product->getInfoProductDefaul($this->getData('product_id_defaul'));
    }
    public function getOMoT_ShowEditPage_Defaul($product_id) {
        return $this->oDb_Product->getOMoT_ShowEditPage_Defaul($product_id);
    }
    public function updateOMoT_defaul($product_id,$aData) {
        return $this->oDb_Product->updateOMoT_defaul($product_id,$aData);
    }
    //======================
    public function updateProductStatus() {
        return $this->oDb_Product->updateProductStatus($this->getAllData());
    }
    public function getInfoProduct() {
        return $this->oDb_Product->getInfoProduct($this->getData('product_id'));
    }
    public function updateProduct($_POST) {
        return $this->oDb_Product->doUpdateByPK($_POST);
    }
    public function updateProduct_OBJ($_POST) {
        return $this->oDb_Product->doUpdateByPK_OBJ($_POST);
    }
    public function  setDataForCheckbox($arrayName,$_POST) {
        foreach ($arrayName as $control) {

            if ($_POST[$control] != "" && $_POST[$control] != null) {
                $_POST[$control] =1;
            }
            else $_POST[$control] =0;


        }
        return $_POST;
    }
    public function filterProduct() {
        return $this->oDb_Product->filterProduct($this);
    }
    public function getImage($product_id) {
        return $this->oDb_Product->getImage($product_id);
    }
    public function updateComment($aData_post, $arr_pictures_id) {
        return $this->oDb_Product->updateComment($aData_post,$arr_pictures_id);
    }
    public function get_infor_pid($product_id) {
        return $this->oDb_Product->get_infor_pid($product_id);
    }
/*
* Created by: tienhv;
* Date: 28th April 2009
*/
    public function updateOMoT($product_id,$aData) {
        return $this->oDb_Product->updateOMoT($product_id,$aData);
    }
    public function getOMoT($product_id) {
        return $this->oDb_Product->getOMoT($product_id);
    }
    public function getOMoT_ShowEditPage($product_id) {
        return $this->oDb_Product->getOMoT_ShowEditPage($product_id);
    }
    public function deleteOMoT($product_id) {
        return $this->oDb_Product->deleteOMoT($product_id);
    }
    public function getParrentAttOfItemProduct($product_id) {
        return $this->oDb_Product->getParrentAttOfItemProduct($product_id);
    }
    public function getAllAttDet() {
        return $this->oDb_Product->getAllAttDet();
    }
    public function getAllAttParr() {
        return $this->oDb_Product->getAllAttParr();
    }
    public function updateAttribute($data) {
        return $this->oDb_Product->updateAttribute($data);
    }
    public function saveAttribute($data) {
        return $this->oDb_Product->saveAttribute($data);
    }
    public function getSize($productID) {
        return $this->oDb_Product->getParrentAttOfItemProduct($productID);
    }

    public function getCatRootPath_Step1($cat_yahoo_id) {
        $rs1=$this->oDb_Product->getCatParent($cat_yahoo_id);
        if(is_array($rs1) && $rs1['product_category_parent_id']>0) {
            $rs=$this->getCatRootPath($rs1['product_category_parent_id']);

            $this->rsCatRootPath[]=$rs;
            $num=-1;
            $cat_id=$rs['product_category_parent_id'];

            while($num!=0) {
                $rs=$this->getCatRootPath($cat_id);
                $cat_id=$rs['product_category_parent_id'];
                $this->rsCatRootPath[]=$rs;
                if(is_array($rs)) {
                    $rs=$this->getCatRootPath($cat_id);
                    $this->rsCatRootPath[]=$rs;
                    $num=$rs['product_category_parent_id'];
                }
                else {
                    break;
                }
            }

            return $this->rsCatRootPath;
        }
    }
    public function getCatRootPath($cat_id) {
        return $this->oDb_Product->getCatInfo($cat_id);
    }

    /**
     * TEMPLATE
     */
    public function getListTemplate() {
        return $this->oDb_Product->getListTemplate($this);
    }
    
    public function getSizeDetail($Id) {
        return $this->oDb_Product->getSizeDetail($Id);
    }

    public function getListSize() {
        return $this->oDb_Product->getListSize();
    }

    public function backUpAfterExortCsv() {
        return $this->oDb_Product->backUpAfterExortCsv($this);
    }

    public function getListExportCsv($condition) {
        return $this->oDb_Product->getListExportCsv($this,$condition);
    }

    public function doExportSingleRow($row = Array()){
        // get province
        $product_province_id =	$row['product_province_id'];
        $getProvinceNamme = $this->getOneProvince($product_province_id);
        $row['product_province_id'] = $getProvinceNamme;
        //product_who_bear_the_CsOfTr
        if (intval($row['product_who_bear_the_CsOfTr']==1)) {
            $row['product_who_bear_the_CsOfTr'] = $this->aLanguage['DATA_SHOW']['Who_wins_the_auction'];
        } else $row['product_who_bear_the_CsOfTr'] = $this->aLanguage['DATA_SHOW']['owner_auction'];
        //product_payment_of_goods_boa
        if (intval($row['product_payment_of_goods_boa'] == 1)) {
            $row['product_payment_of_goods_boa'] = $this->aLanguage['DATA_SHOW']['payment_of_goods_before'];
        } else $row['product_payment_of_goods_boa'] = $this->aLanguage['DATA_SHOW']['payment_of_goods_after'];
        //product_payment_method, payment htrough bank
        if (intval($row['product_payment_method'] == 1 )) {
            $row['product_payment_method'] = $this->aLanguage['DATA_SHOW']['payment_throught_bank_1'];
            $arrayBankName = explode(">*",$row['product_bank']);
            $count_bank_name = count($arrayBankName);

            for($i=1;$i<=$count_bank_name;$i++) {
                $item_bank = "product_bank_".$i;
                $row[$item_bank] =$arrayBankName[$i-1];
            }
            for($i=$count_bank_name+1;$i<=10;$i++) {
                $item_bank = "product_bank_".$i;
                $row[$item_bank] ='    ';
            }

            $csv_bank='';
            for($i=1;$i<=10;$i++) {
                $item_bank = "product_bank_".$i;
                $row[$item_bank] = mb_convert_encoding($row[$item_bank], "SJIS", "UTF-8");
                $csv_bank .= $row[$item_bank]." ,";
            }

        } else { $csv_bank='';
            $row['product_payment_method'] = $this->aLanguage['DATA_SHOW']['payment_throught_bank_0'];
            for($i=1;$i<=10;$i++) {
                $item_bank = "product_bank_".$i;
                $row[$item_bank] =' ';
                $csv_bank .= $row[$item_bank].",";
            }
        }
        $csv_bank = substr($csv_bank,0,strlen($csv_bank)-1);

        //product_send_cash
        if (intval($row['product_send_cash']==1)) {
            $row['product_send_cash'] = $this->aLanguage['DATA_SHOW']['product_send_cash_1'];
        } else $row['product_send_cash'] =	$this->aLanguage['DATA_SHOW']['product_send_cash_0'];
        //product_POD
        if (intval($row['product_POD']==1)) {
            $row['product_POD'] = $this->aLanguage['DATA_SHOW']['product_POD_1'];
        } else $row['product_POD']	= $this->aLanguage['DATA_SHOW']['product_POD_0'];
        //product_credit_card
        if (intval($row['product_credit_card']==1)) {
            $row['product_credit_card'] = $this->aLanguage['DATA_SHOW']['product_credit_card_1'];
        } else $row['product_credit_card'] = $this->aLanguage['DATA_SHOW']['product_credit_card_0'];

        //Other payment
        if ($row['product_other_payment_method']!='') {

            $arrayOtherPM = explode(">*",$row['product_other_payment_method']);
            $count_Other = count($arrayOtherPM);

            for($i=1;$i<=$count_Other;$i++) {
                $item_other = "product_other_payment_method_".$i;
                $row[$item_other] =$arrayOtherPM[$i-1];
            }
            for($i=$count_Other+1;$i<=10;$i++) {
                $item_other = "product_other_payment_method_".$i;
                $row[$item_other] =' ';
            }

        } else{
            for($i=1;$i<=10;$i++) {
                $item_bank = "product_other_payment_method_".$i;
                $row[$item_bank] =' ';
            }
        }
        $csv_other_payment_method = '';
        for($i=1;$i<=10;$i++) {
            $item_other = "product_other_payment_method_".$i;
            $row[$item_other] =	mb_convert_encoding($row[$item_other],"SJIS","UTF-8");
            $csv_other_payment_method .= $row[$item_other]." ,";
        }

        $csv_other_payment_method = substr($csv_other_payment_method, 0, strlen($csv_other_payment_method)-1);

        $csv_other_trans='';
        $aDataOMoT = $this->getOMoT($row['product_id']);
        $count_OtherMO = 0;

        if(is_array($aDataOMoT)) {
            $count_OtherMO = count($aDataOMoT);
            foreach ($aDataOMoT as $dataOMoT) {
                $item_other_title =  mb_convert_encoding($dataOMoT['other_title_of_method'],"SJIS","UTF-8");
                $item_link_to_price_list =  mb_convert_encoding($dataOMoT['other_link_to_price_list'],"SJIS","UTF-8");
                $item_uniform_price =  mb_convert_encoding($dataOMoT['other_uniform_price'],"SJIS","UTF-8");
                $csv_other_trans .= $item_other_title.' ,'.$item_link_to_price_list.' ,'.$item_uniform_price.' ,';
            }
        }

        for($i=$count_OtherMO+1;$i<=10;$i++)
            $csv_other_trans.= " , , ,";

        $csv_other_trans =substr($csv_other_trans, 0, strlen($csv_other_trans)-1);

        //product_product_status
        if (intval($row['product_product_status'])==1)
            $row['product_product_status'] = $this->aLanguage['DATA_SHOW']['product_SecondHand'];
        elseif (intval($row['product_product_status'])==2)
            $row['product_product_status'] = $this->aLanguage['DATA_SHOW']['product_new'];
        else
            $row['product_product_status'] = $this->aLanguage['DATA_SHOW']['product_other'];

        if (intval($row['product_ability_to_return_product']==1)) {
            $row['product_ability_to_return_product'] =	$this->aLanguage['DATA_SHOW']['product_ability_to_return_product_1'];
        } else $row['product_ability_to_return_product'] = $this->aLanguage['DATA_SHOW']['product_ability_to_return_product_0'];

        $row['product_explanation'] = ereg_replace("/\n\r|\r\n|\n|\r/", "", $row['product_explanation']);
        $row['product_description'] = preg_replace("/\t/", "", $row['product_explanation']);

        $row['product_size'] = ereg_replace("/\n\r|\r\n|\n|\r/", "", $row['product_size']);
        $row['product_description'].='<br/>'.preg_replace("/\t/", "", $row['product_size']);

        $row['product_rank'] = ereg_replace("/\n\r|\r\n|\n|\r/", "", $row['product_rank']);
        $row['product_description'].= '<br/>'.preg_replace("/\t/", "", $row['product_rank']);

        $row['product_note'] = ereg_replace("/\n\r|\r\n|\n|\r/", "", $row['product_note']);
        $row['product_description'].='<br/>'. preg_replace("/\t/", "", $row['product_note']);

        if (intval($row['product_auto_extend'] == 1)) {
            $row['product_auto_extend'] = $this->aLanguage['DATA_SHOW']['product_auto_extend_1'];
        } else $row['product_auto_extend'] = $this->aLanguage['DATA_SHOW']['product_auto_extend_0'];

        if (intval($row['product_end_soon'] == 1)) {
            $row['product_end_soon'] =	$this->aLanguage['DATA_SHOW']['product_end_soon_1'];
        } else $row['product_end_soon']	= $this->aLanguage['DATA_SHOW']['product_end_soon_0'];

        if (intval($row['product_have_to_negotiate_discount']==1)) {
            $row['product_have_to_negotiate_discount'] = $this->aLanguage['DATA_SHOW']['product_have_to_negotiate_discount_1'];
        } else $row['product_have_to_negotiate_discount'] = $this->aLanguage['DATA_SHOW']['product_have_to_negotiate_discount_0'];

        if (intval($row['product_title_bold_line']==1)) {
            $row['product_title_bold_line'] = $this->aLanguage['DATA_SHOW']['product_title_bold_line_1'];
        } else $row['product_title_bold_line'] = $this->aLanguage['DATA_SHOW']['product_title_bold_line_0'];

        if (intval($row['product_store_hot_auction']==1)) {
            $row['product_store_hot_auction'] =	$this->aLanguage['DATA_SHOW']['product_store_hot_auction_1'];
        } else $row['product_store_hot_auction'] = $this->aLanguage['DATA_SHOW']['product_store_hot_auction_0'];

        if (intval($row['product_bakground_color'] == 1)) {
            $row['product_bakground_color'] = $this->aLanguage['DATA_SHOW']['product_bakground_color_1'];
        } else $row['product_bakground_color'] = $this->aLanguage['DATA_SHOW']['product_bakground_color_0'];

        //if (intval($row['product_icon_highlight']==1)) {
        //				$row['product_icon_highlight']		=	$this->aLanguage['DATA_SHOW']['product_icon_highlight_1'];
        //			} else $row['product_icon_highlight']	=	$this->aLanguage['DATA_SHOW']['product_icon_highlight_0'];

        if (intval($row['product_icon_gift']==1)) {
            $row['product_icon_gift'] =	$this->aLanguage['DATA_SHOW']['product_icon_gift_1'];
        } else $row['product_icon_gift'] = $this->aLanguage['DATA_SHOW']['product_icon_gift_0'];

        //if (intval($row['product_yahoo_point']==1)) {
        //				$row['product_yahoo_point']		=	$this->aLanguage['DATA_SHOW']['product_yahoo_point_1'];
        //			} else $row['product_yahoo_point']	=	$this->aLanguage['DATA_SHOW']['product_yahoo_point_0'];


        if (intval($row['product_affiliate']==1)) {
            $row['product_remuneration_to_partner'] = $row['product_remuneration_to_partner']	;
        } else $row['product_remuneration_to_partner']	=	'';
        if (intval($row['product_use_yahoo']==1)) {
            $row['product_use_yahoo'] = $row['product_use_yahoo_1']	;
        } else $row['product_use_yahoo'] = $row['product_use_yahoo_0']	;
        if (intval($row['product_transport_goods_abroad']==1)) {
            $row['product_transport_goods_abroad'] = $this->aLanguage['DATA_SHOW']['product_transport_goods_abroad_1'];
        } else {
            $row['product_transport_goods_abroad'] = $this->aLanguage['DATA_SHOW']['product_transport_goods_abroad_0'];
        }
        /**
         * display image
         */
        //$arr_image=$this->getImage($row['product_id']);

        if($row['product_picture1']!="") {
            $file = PATH_UPLOAD_PRODUCT.$row['product_picture1'];
            $newfile = PATH_ROOT.'auctions/'.$row['product_picture1'];
            @copy($file,$newfile);
        }
        if($row['product_picture2']!="") {
            $file = PATH_UPLOAD_PRODUCT.$row['product_picture2'];
            $newfile = PATH_ROOT.'auctions/'.$row['product_picture2'];
            @copy($file,$newfile);
        }
        if($row['product_picture3']!="") {
            $file = PATH_UPLOAD_PRODUCT.$row['product_picture3'];
            $newfile = PATH_ROOT.'auctions/'.$row['product_picture3'];
            @copy($file,$newfile);
        }

        $row['product_auction_english_version'] = "";// not exist in data base
        $row['product_description_for_english_version'] = "";
        $row['product_title_of_english_version'] = "";

        $arrayNameKey = array_keys($row);

        // convert data k aja
        $key_select=$row['product_key_select'];
        if($key_select==0) {
            $row['product_new_title']=$row['product_store'].$row['product_title']. " " . $row['product_id'];
        } else {
            $row['product_new_title']=$row['product_key'.$key_select]." ".$row['product_store'].$row['product_title']. " " . $row['product_id'];
        }
        $row['product_new_title']=mb_convert_encoding($row['product_new_title'],"SJIS","UTF-8");
        foreach ($arrayNameKey as  $filed) {
            if($row[$filed]==" ") {
                $row[$filed]="";
            }
            $row[$filed]=trim($row[$filed]);
            if(!is_numeric($row[$filed]) && $row[$filed]!="" && $filed!="product_description_on_mobile" && $filed!="product_description") {
                $row[$filed]='"'.$row[$filed].'"';
            }
            $row[$filed]=mb_convert_encoding($row[$filed],"SJIS","UTF-8");
        }
        if($row['product_periods']==0) {
            $row['product_periods']="";
        }
        $row['product_description_on_mobile'] = '"'.str_replace('"','""',$row['product_description_on_mobile']).'"';
        $row['product_description'] = '"'.str_replace('"','""',$row['product_description']).'"';

        $csv .= $row['product_id'].',';
        $csv .= $row['product_category_id'].',"';
        $csv .= $row['product_new_title'].'",';
        $csv .= $row['product_description'].',';
        $csv .= $row['product_description_on_mobile'].',';
        $csv .= $row['product_keyword_commodity'].',';
        $csv .= $row['product_prices_start'].',';
        $csv .= $row['product_qty'].',';
        $csv .= $row['product_periods'].',';
        $csv .= $row['product_time_auction'].',';
        $csv .= $row['product_time_auction_end'].',';
        $csv .= $row['product_province_id'].',';
        $csv .= $row['product_shipping_area'].',';
        $csv .= $row['product_who_bear_the_CsOfTr'].',';
        $csv .= $row['product_payment_of_goods_boa'].',';
        $csv .= $row['product_payment_method'].',';
        $csv .= $csv_bank.',';
        $csv .= $row['product_send_cash'].',';
        $csv .= $row['product_POD'].',';
        $csv .= $row['product_credit_card'].',';
        $csv .= $csv_other_payment_method.',';
        $csv .= $row['product_product_status'].',';
        $csv .= $row['product_product_status_comment'].',';
        $csv .= $row['product_ability_to_return_product'].',';
        $csv .= $row['product_ability_to_return_product_comment'].',';

        $csv .= $row['product_picture1'].',';
        $csv .= $row['product_picture_comment1'].',';
        $csv .= $row['product_picture2'].',';
        $csv .= $row['product_picture_comment2'].',';
        $csv .= $row['product_picture3'].',';
        $csv .= $row['product_picture_comment3'].',';

        $csv .= $row['product_limited_assesment_of_the_bidder'].',';
        $csv .= $row['product_auto_extend'].',';
        $csv .= $row['product_end_soon'].',';
        $csv .= $row['product_price_fastener'].',';
        $csv .= $row['product_have_to_negotiate_discount'].',';
        $csv .= $row['product_auto_open_aution'].',';
        $csv .= $row['product_price_wins_minimum'].',';
        $csv .= $row['product_auction_notable'].',';
        $csv .= $row['product_title_bold_line'].',';
        $csv .= $row['product_bakground_color'].',';
        $csv .= $row['product_store_hot_auction'].',';
        $csv .= $row['product_icon_highlight'].',';
        $csv .= $row['product_icon_gift'].',';
        $csv .= $row['product_yahoo_point'].',';
        $csv .= $row['product_remuneration_to_partner'].',';
        $csv .= $row['product_size_after_packet'].',';
        $csv .= $row['product_weight_after_packet'].',';
        $csv .= $row['product_use_yahoo'].',';
        $csv .= $csv_other_trans.',';
        $csv .= $row['product_transport_goods_abroad'].',';
        $csv .= $row['product_auction_english_version'].',';
        $csv .= $row['product_title_of_english_version'].',';
        $csv .= $row['product_description_for_english_version'];

        $csv .= "\n";
        $arr_data = null;
        $arr_data = explode(',', $csv);
        return $csv;
    }
}
